from flask import request,Flask,render_template
import time,random,os
app = Flask(__name__) 
BIG="ABCDEFGHIJKLMNOPQRSTUVWXYZ.\\/?<>\"''{}[]=+*,;:%^&@#~!"

ap=[0,0,0,0]

@app.route('/', methods = ["get"])
def post_data():
    return render_template("main.html")

@app.route('/goin', methods = ["get"])
def goin():
    return render_template("in.html")

@app.route('/tologin', methods = ["get"])
def tologin():
    return render_template("tologin.html")

@app.route('/login', methods = ["get"])
def login():
    return render_template("login.html")

@app.route('/goout', methods = ["get"])
def goout():
    return render_template("out.html")

@app.route('/left', methods = ["get"])
def left():
    return render_template("left.html")

@app.route('/have', methods = ["post"])
def dataa():#nm,pw):
    sddaa = request.get_json()
    for sd in sddaa.values():
        if sd=="":
            return "失败：不能为空"
    nm=sddaa["havename"]
    pw=sddaa["havepswd"]
    try:
        open("data/%s/msg"%nm,"r",encoding='utf-8')
    except:
        return "失败：用户名错误"
    
    f=open("data/%s/pswd"%nm,"r",encoding='utf-8')
    if pw != f.read():
        return "失败：密码错误"
    f.close()
    
    f=open("data/%s/msg"%nm, "r",encoding='utf-8')
    nr=f.read()+"<a name=bottom></a>"##bottom,<a name=bottom></a>
    f.close()
    
    return nr

@app.route('/log', methods = ["post"])
def log():#nm,pw):
    logi = request.get_json()
    for sd in logi.values():
        if sd=="":
            return "失败：不能为空"
    nm=logi["logname"]
    for i in nm:
        if i in BIG:
            return "失败：有违规字符\nABCDEFGHIJKLMNOPQRSTUVWXYZ.\\/?<>\"''{}[]=+*,;:%^&@#~!"
    pw=logi["logpswd"]
    for i in pw:
        if i in BIG:
            return "失败：有违规字符\nABCDEFGHIJKLMNOPQRSTUVWXYZ.\\/?<>\"''{}[]=+*,;:%^&@#~!"
    
    if os.path.exists("data/"+nm):
        return "失败：用户已存在"
    
    os.mkdir("data/"+nm)
    
    f=open("data/%s/msg"%nm,"w",encoding='utf-8')
    f.write("<h3>系统提示</h3>\n<p>恭喜你，创建用户成功</p>\n<p></p>\n")
    f.close()
    
    f=open("data/%s/pswd"%nm,"w",encoding='utf-8')
    f.write(pw)
    f.close()
    
    return "创建成功！"

@app.route('/send', methods = ["post"])
def sendd():
    sdda = request.get_json()
    for sd in sdda.values():
        if sd=="":
            return "失败：不能为空"
    
    try:
        open("data/%s/msg"%sdda["outname"],"r",encoding='utf-8')
        open("data/%s/msg"%sdda["inname"],"r",encoding='utf-8')
    except:
        return "失败：用户名错误"
    
    f=open("data/%s/pswd"%sdda["outname"],"r",encoding='utf-8')
    if sdda["outpswd"] != f.read():
        return "失败：密码错误"
    f.close()
    
    msg="<h3>来自用户 "+sdda["outname"]+" </h3>\n<p>"+sdda["t"]+"</p>\n<p></p>\n"
    
    
    with open("data/%s/msg"%sdda["inname"], "r+",encoding='utf-8') as f:
        old = f.read()  # 先读取内容保存一份
        f.seek(0)  # 将光标定位到起始位置
        f.write(msg)  # 写入新的数据，现在写入时，会清除覆盖掉原有数据
        f.write(old)  #写入之前数据，这次在写入数据时，不会再清除原有数据，而是在尾部添加
    
    return "发送成功"

app.run(host="0.0.0.0",port="7004",debug=True)
    